<template>
  <div class="container">
    <video class="bg" autoplay  muted src="@/assets/bj.mp4"></video>
    <div class="card">
      <div class="register" ref="register">
        <p class="p1">已有有账号？</p>
        <p class="p2">登录探索我们的世界</p>
        <div class="div1" id="login">登录</div>
      </div>
       <div class="login" ref="login">
         <p class="p1">还没有账号？</p>
         <p class="p2">注册即代表同意用户协议</p>
         <div class="div1" id="register">注册</div>
       </div>
       <div class="scroll" ref="scroll">
        <div class="registerForm" ref="registerFormDiv" style="display:none">
 <div class="header">注册</div>
        <el-form ref="registerForm" :model="formData" :rules="rules" label-width="70px" class="register">
          <el-form-item label="用户名" prop="username">
            <el-input placeholder="输入用户名" v-model="formData.username"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input placeholder="输入邮箱" v-model="formData.email"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input placeholder="输入密码" v-model="formData.password"></el-input>
          </el-form-item>
         <el-row justify="center" type="flex">
          <el-button class="submit" size="small" @click="registerUser" type="primary">注册</el-button>
          <el-button class="reset" size="small" @click="resetForm">重置</el-button>
         </el-row>
        </el-form>
        </div>

       <div class="loginForm" ref="loginFormDiv">
        <div class="header">登录</div>
        <el-form :model="formData" ref="loginForm" :rules="rules" label-width="70px" class="login">
          <el-form-item label="用户名" prop="username">
            <el-input placeholder="输入用户名或者邮箱" v-model="formData.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input placeholder="输入密码" v-model="formData.password"></el-input>
          </el-form-item>
         <el-row justify="center" type="flex">
          <el-button class="submit" size="small" type="primary" @click="loginUser">登录</el-button>
          <el-button class="reset" size="small" @click="resetForm">重置</el-button>
         </el-row>
        </el-form>
       </div>

       </div>
    </div>
  </div>
</template>

<script>
import { register } from '@/api/user'
export default {
  data () {
    return {
      formData: {
        username: '',
        email: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '用户名必填', trigger: 'blur' }],
        password: [
          { required: true, message: '密码必填', trigger: 'blur' },
          { required: true, min: 6, message: '密码6-10个字符', max: 10, trigger: 'blur' }
        ],
        email: [{ required: true, message: '邮箱必填', trigger: 'blur' },
          { pattern: /^\d{5,12}@[qQ][qQ]\.(com|cn)$/, message: '邮箱不正确', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    // 获取非表单登录按钮
    const loginDom = document.querySelector('#login')
    // 获取非表单注册按钮
    const registerDom = document.querySelector('#register')
    // 获取平移的scroll
    const scroll = document.querySelector('.scroll')
    // 获取登录非表单
    const login = document.querySelector('.login')
    const register = document.querySelector('.register')
    // 获取表单
    const loginForm = document.querySelector('.loginForm')
    const registerForm = document.querySelector('.registerForm')
    loginDom.addEventListener('click', function () {
      // 重复代码可优化

      scroll.style.left = 20 + 'px'
      register.style.opacity = 0
      login.style.opacity = 1
      registerForm.style.display = 'none'
      loginForm.style.display = 'block'
    })
    registerDom.addEventListener('click', function () {
      // 重复代码可优化

      register.style.opacity = 1
      login.style.opacity = 0
      registerForm.style.display = 'block'
      loginForm.style.display = 'none'
      scroll.style.left = 290 + 'px'
    })
  },
  methods: {
    // 注册用户
    async registerUser () {
      await this.$refs.registerForm.validate()
      await register(this.formData)
      this.$nextTick(() => {
        // 重复代码可优化
        this.$refs.registerFormDiv.style.display = 'none'
        this.$refs.loginFormDiv.style.display = 'block'
        this.$refs.scroll.style.left = 20 + 'px'
        this.$refs.login.style.opacity = 1
      })
    },
    // 用户登录
    async loginUser () {
      await this.$refs.loginForm.validate()
      await this.$store.dispatch('user/login', {
        identifier: this.formData.username,
        password: this.formData.password
      })
      this.$router.push('/index')
    },
    // 重置表单
    resetForm () {
      this.$refs.loginForm.resetFields()
      this.formData.email = ''
    }

  }
}
</script>

<style lang="scss" scoped>
.bg{
  position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 100%;
height: 100%;
object-fit: cover;
min-height: 600px;
z-index: -9;
}
.container{
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.card{
  width: 600px;
  height: 280px;
  background-color: #122129;
  display: flex;
  color: white;
  position: relative;
  .submit{
    background-color: #1a2c38;
    border: none;
  }
  .reset{
    border: 1px solid #122129;
  }
  .reset:hover{
     background-color: #ccc;
     color: #1a2c38;
  }
  .login,.register{
    padding: 0px 40px;
    flex: 1;
    opacity: 1;
    transition: all .5s ease-in;
    .p1{
      font-size: 25px;
      margin-top: 50px;
      margin-bottom: 20px;
    }
    .p2{
      margin-bottom: 30px;
    }
    .div1{
      border: 1px solid #ccc;
      width: 60px;
      height: 40px;
      border-radius: 20%;
      text-align: center;
      line-height: 40px;
      cursor: pointer;
      &:hover{
        color: #122129;
        background-color: white;
      }
    }
  }
  .scroll{
   position: absolute;
   height: 360px;
   top: -40px;
   width: 290px;
   left: 20px;
   background-color: rgb(255, 255, 255);
   transition: all .6s ease-in-out;
   .register,.login{
      width: 270px;
      display: flex;
      flex-direction: column;
      margin-top: 40px;
      padding: 0;
   }
   .header{
    height: 40px;
    background-color: #1a2c38;
    color: white;
    text-align: center;
    line-height: 40px;
    font-weight: 700;
   }
  //  &:hover{
  //   left: 290px;
  //  }
  }
}
.loginForm{

}
.card .scroll .login{
    margin-top: 70px;
  }
</style>
